import VueRouter from 'vue-router'
import Router from 'vue-router'
import index from '../views/layout/index'
import login from "../views/login";
import errorPage from "../views/error"
import {getToken} from "@/api/session";
import Vue from 'vue'
import roles from "../views/roles/index"
import retail from "../views/retail/index"
import purchase from "../views/purchase/index"
import sell from "../views/sell/index"
import warehouse from "../views/warehouse/index"
import about from "../views/about/index"
import dash from "../views/dashBoard/index"
// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题:Avoided redundant navigation to current location: “/XXX“
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)

/*设置vue路由映射*/
/*第一层component填充app.vue中的router view*/
const routes = [
    //下面的这四个对应导航栏的四个模块，其中的children则是子菜单
    //因为第一层的component index用于填充App.vue,而children则填充layout/index.vue
    //最好不要乱改path，要在store/*.js 下更新常量
    {
        name: 'admin',
        component: index,
        path: '/',
        redirect: '/dashBoard',
        menus: ['首页', '零售管理', '采购管理', '销售管理', '仓库管理', '权限管理', '相关信息'], /*自定义属性，admin后台的菜单项名*/
        icons: ['el-icon-s-promotion', 'el-icon-s-management', 'el-icon-s-goods', 'el-icon-sold-out', 'el-icon-s-home', 'el-icon-user-solid', 'el-icon-chat-line-round'], /*对应的图标*/
        pathIndex: [[0], [1], [2], [3], [4], [5], [6], [7]], /*每个菜单的子菜单在children中对应的index*/
        children: [
            {
                path: '/dashBoard',
                name: '首页',
                component: dash,
                meta: {title: "首页"},
            },
            {
                path: '/retail',
                name: '零售管理',
                component: retail,
                meta: {title: "retail"},
            },
            {
                path: '/purchase',
                name: '采购管理',
                component: purchase,
                meta: {title: "purchase"},
            },
            {
                path: '/sell',
                name: '销售管理',
                component: sell,
                meta: {title: "sell"},
            },
            {
                path: '/warehouse',
                name: '仓库管理',
                component: warehouse,
                meta: {title: "warehouse"},
            },
            {
                path: '/roles',
                name: '权限管理',
                component: roles,
                meta: {title: "roles"},
            },
            {
                path: '/about',
                name: 'info',
                component: about,
                meta: {title: "about"},
            },
        ]
    },
    {
        path: '/login',
        meta: {title: 'login'},
        component: login,
        name: 'login'
    },
    {
        path: '*',
        component: errorPage
    }
]

const router = new VueRouter({
    mode: 'hash',
    base: process.env.BASE_URL,
    routes
})


router.beforeEach((to, from, next) => {//beforeEach是router的钩子函数，在进入路由前执行
    if (getToken() != null) {
        if (to.path === '/login')
            next('/');
        else
            next();
    } else {
        if (to.path === '/login')
            next();
        else
            next('/login')
    }
});

export default router
